/**
 * 
 */

/**
 * 初始化分页插件
 * @param curPage
 * @param totalPages 
 */
function initLogListPage(path,curPage,totalPages,tp){ 
	
	if(totalPages==0){
		totalPages=1; 
	}
	
	var options = {
			currentPage : curPage,
			totalPages : totalPages,
			numberOfPages : 10,  
			size:"normal",
			alignment:"right",  
			/*pageUrl : function(type, page, current) { 
				return path + "/log_list.action?page=" + page;
			}*/
			onPageClicked: function(e,originalEvent,type,page){  
//				searchLogs(path,page,$("#datetimepicker_mask_start").val(),$("#datetimepicker_mask_end").val(),$("#dropdownMenu1").text());
				refreshLogTable(path,page,$("#datetimepicker_mask_start").val(),$("#datetimepicker_mask_end").val(),geTp($("#dropdownMenu1").text()));
            }
		};
		$('#log_list_pager').bootstrapPaginator(options);
}

/* 获取所有选中项的id字符串   */
function getChecked() {  
	var ids="";
	$("input[type=checkbox]").each(function() {
		if ($(this).prop("checked")) {
			ids+=$(this).val()+",";
		} 

	});
	return ids.substr(0,ids.length-1);
}



/**
 * 删除选中
 */
function delLogs(path){
	var ids=getChecked();
	if(ids.length>0){
		delLog(path,ids);
	}else{
		alertError("请选中删除项");
	}
	return false;
}

/**
 * 删除 logId
 * @param logId
 */ 
function delLog(path,logId){
	$.ajax({ 
		type:"POST",
		url:path+"log_deleteLogsByIds.action", 
		data:{
			ids:logId
		},
		dataType:"json",
		success:function(result){ 
			if(result.success){    
//				window.location.href=path+"log_list.action";
				refreshLogTable(path,1,$("#datetimepicker_mask_start").val(),$("#datetimepicker_mask_end").val(),geTp($("#dropdownMenu1").text()));
				alertSuccess("删除成功");  
			}else{
				alertError("删除失败");
			} 
		}
	});
}

/**
 * 条件查询日志 
 * @param path
 * @param page
 * @param st
 * @param et
 * @param tp
 */
function searchLogs(path,page,st,et,tp){
	$.ajax({ 
		type:"POST", 
		url:path+"log_list.action?page="+page,
		data:{
			startTime:st,
			endTime:et,
			tp:tp
		},
		dataType:"json",
		success:function(result){
			if(result.success){
//				window.location.href=path+"log_list.action";
				 refreshLogTable(path,page,st,et,tp);
			}else{
				alertError("删除失败");  
			}
		}
	});
}
/**
 * 重新加载log 表格
 * @param path
 * @param page
 * @param st 
 * @param et
 * @param tp
 */
function refreshLogTable(path,page,st,et,tp){
	 
	/* 清空数据 ， 取消选中 */
	$("#log_list_table_tbody").empty();
	$("input[type=checkbox]").removeAttr("checked");
	
	$.ajax({ 
		type:"POST",  
		url:path+"log_list.action?page="+page,
		data:{
			startTime:st, 
			endTime:et, 
			tp:tp
		},
		dataType:"json",
		success:function(result){
			if(result.success){  
//				window.location.href=path+"log_list.action"; 
//				alertSuccess("删除成功"); 
				var loginfos=result.data;
				if(loginfos.length==0){ 
					$("#log_list_table_tbody").empty();
				}
				for(var i=0;i<loginfos.length;i++){ 
					var loginfo=loginfos[i]; 
					var $e=$('<tr><td class=" hidden-xs"style="text-align:center;"><input type=checkbox name="abc" value="'+loginfo.uuid+
							'"></td><td class="" style="text-align:center;">'+loginfo.priority+'</td><td class=" hidden-xs" style="text-align:center;">'+
							loginfo.logDate+'</td><td class=" " style="text-align:center;">'+loginfo.rmseconds+
							'</td> <td class="" style="text-align:center;"><a href="'+path+'log_details.action?id='+loginfo.uuid +
							'" class="btn btn-warning">详情</a> '//+
//							'| <a  href="javascript:void(0)" onclick="delLog("a","'+loginfo.uuid+'")" id="log_list_del" >删除</a></td></tr>'
							);
					$("#log_list_table_tbody").append($e);
				}
				
				initLogListPage(path,result.pageBean.curPage,result.pageBean.totalPages,tp);
				
				 
			}else{
				alertError("操作失败");
			}
		}
	});
}

function geTp(v){
	if(v.trim()=="DEBUG"||v.trim()=="INFO"||v.trim()=="WARN"||v.trim()=="ERROR"||v.trim()=="FATAL"){
		return v;
	}else{
		return "";
	}
}
